/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "accessor.h"

using namespace dev;

bool Accessor::init(mc::mutable_dict& csr_object, const mc::dict& connector) {
    try {
        // 验证必需的CSR字段
        if (!csr_object.contains("Chip") || !csr_object.contains("Offset") ||
            !csr_object.contains("Size") || !csr_object.contains("Mask") ||
            !csr_object.contains("Type")) {
            elog("Accessor init failed: missing required CSR fields");
            return false;
        }

        from_variant(csr_object, *this);
        i_accessor.init_accessor_signal();
    } catch (const std::exception& e) {
        elog("Accessor init failed, exception: ${exception}", ("exception", e.what()));
        return false;
    }
    return true;
}

// 自定义反序列化：将扁平结构映射到 i_accessor 成员
void dev::Accessor::from_variant(const mc::dict& d, dev::Accessor& obj) {
    mc::from_variant(d, obj.i_accessor);
}

MC_REFLECT(dev::Accessor, (i_accessor, "Accessor"))
